package com.bbcare.nutritious.plat.controller;

import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.lang.StringUtils;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.bbcare.comm.AppException;
import com.bbcare.comm.Constants;
import com.bbcare.comm.ErrorCode;
import com.bbcare.comm.PropertiesUtils;
import com.bbcare.comm.ResultModel;
import com.bbcare.nutritious.plat.service.INutritionSchemeUsedService;

/**
 * 
 * @name:         AppNutritionUsedController.java
 * @author:       tao
 * @createTime:   2017年11月20日
 * @description:  app营养推送和上传 
 * @modify:
 *
 */
@Controller
@RequestMapping("/webapi/service/nutrition")
public class AppNutritionUsedController {
	
    public static final Logger logger = Logger.getLogger(AppNutritionUsedController.class);
    
    @Autowired
    private INutritionSchemeUsedService schemeUsedService;

    @ResponseBody
    @RequestMapping("/schedule/query")
    public ResultModel<Object> patientScheduleQry(HttpServletRequest request, @RequestBody String jsonStr)
            throws Exception {
        logger.info("in patientScheduleQry----arg==" + jsonStr);
        JSONObject jsonEsb = JSON.parseObject(jsonStr);
        JSONObject requestParam = jsonEsb.getJSONObject(Constants.INF_INPUTPARAM_COMMONFIELD); // 统一节点处理
        if (null == requestParam) {
            throw new AppException(ErrorCode.FOLLOWUP_EXCEPTION_TYPE_INPUT_DATA_VERIFY_ERROR, "入参节点错误，无data节点！");
        }
        
        String userId = requestParam.getString("userId");
        if(StringUtils.isEmpty(userId)){
        	logger.debug("out patientScheduleQry----arg== null");
        	throw new AppException(ErrorCode.FOLLOWUP_EXCEPTION_TYPE_INPUT_DATA_VERIFY_ERROR,"入参校验失败，用户ID不能为空");
        }
        
        /*channel渠道类型必传字段app登陆时特殊处理标识，跳过登陆*/
		String channel = requestParam.getString("channel");
		if(null == channel || !PropertiesUtils.getProperty("channel").equals(channel)){
			throw new AppException(ErrorCode.FOLLOWUP_EXCEPTION_TYPE_INPUT_DATA_VERIFY_ERROR, "channel传入为空或者传入值不正确!");
    	}
		
		Map<String,Object> rspMap = schemeUsedService.patientScheduleQry(requestParam);
        
        logger.debug("out patientScheduleQry----arg=="+JSON.toJSONString(rspMap));
        return new ResultModel<Object>(rspMap);
    }
    
    @ResponseBody
    @RequestMapping("/schedule/upload")
    public ResultModel<Object> patientScheduleUpload(HttpServletRequest request, @RequestBody String jsonStr)
            throws Exception {
        logger.info("in patientScheduleUpload----arg==" + jsonStr);
        JSONObject jsonEsb = JSON.parseObject(jsonStr);
        JSONObject requestParam = jsonEsb.getJSONObject(Constants.INF_INPUTPARAM_COMMONFIELD); // 统一节点处理
        if (null == requestParam) {
            throw new AppException(ErrorCode.FOLLOWUP_EXCEPTION_TYPE_INPUT_DATA_VERIFY_ERROR, "入参节点错误，无data节点！");
        }
        
        String situationId = requestParam.getString("situationId");
        if(StringUtils.isEmpty(situationId)){
        	logger.debug("out patientScheduleUpload----arg== null");
        	throw new AppException(ErrorCode.FOLLOWUP_EXCEPTION_TYPE_INPUT_DATA_VERIFY_ERROR,"situationId不能为空");
        }
        
        /*String isComplete = requestParam.getString("isComplete");
        if(StringUtils.isEmpty(isComplete)){
        	logger.debug("out patientScheduleUpload----arg== null");
        	throw new AppException(ErrorCode.FOLLOWUP_EXCEPTION_TYPE_INPUT_DATA_VERIFY_ERROR,"是否完成不能为空");
        }*/
        /*channel渠道类型必传字段app登陆时特殊处理标识，跳过登陆*/
		String channel = requestParam.getString("channel");
		if(null == channel || !PropertiesUtils.getProperty("channel").equals(channel)){
			throw new AppException(ErrorCode.FOLLOWUP_EXCEPTION_TYPE_INPUT_DATA_VERIFY_ERROR, "channel传入为空或者传入值不正确!");
    	}
		
        Map<String,Object> rspMap = schemeUsedService.patientScheduleUpload(requestParam);
        
        logger.debug("out patientScheduleUpload----arg=="+JSON.toJSONString(rspMap));
        return new ResultModel<Object>(rspMap);
    }
}
